#!/usr/bin/python2
# -*- coding: utf8 -*-
from PyQt4 import QtCore, QtGui, Qt, QtDeclarative
from models.BaseTimeModel import BaseTimeModel
from utils.log import log

class MonthCalendarModel(BaseTimeModel):
    DAY_ROLE = 0
    MONTH_ROLE = 1
    WEEK_DAY_ROLE = 2

    _fromDate = QtCore.QDate.currentDate()
    _currentDate = QtCore.QDateTime.currentDateTime()
    dateChange = QtCore.pyqtSignal()
    _count = 0;

    def __init__(self, fromDate=QtCore.QDateTime.currentDateTime(), parent=None):
        BaseTimeModel.__init__(self, fromDate, parent)
        self.setRoleNames({self.DAY_ROLE: 'day', self.MONTH_ROLE: 'month', self.WEEK_DAY_ROLE: 'dayOfWeek'})
        self.change(fromDate)

    def data(self, index, role):
        if not index.isValid: return QtCore.QVariant()
        if role == self.DAY_ROLE:
            day = self._fromDate.addDays(index.row())
            return QtCore.QVariant(day.day())
        if role == self.MONTH_ROLE:
            day = self._fromDate.addDays(index.row())
            return QtCore.QVariant(day.month())
        if role == self.WEEK_DAY_ROLE:
            day = self._fromDate.addDays(index.row())
            return QtCore.QVariant(day.dayOfWeek())
        return QtCore.QVariant()

    def rowCount(self, parent=QtCore.QModelIndex()):
        return self._count

    def change(self, dateTime):
        date = dateTime.date()
        time = dateTime.time()
        monthBegin  = QtCore.QDate(date.year(), date.month(), 1)
        dayOfWeek = monthBegin.dayOfWeek()
        self._currentDate = dateTime
        self._fromDate = QtCore.QDate(monthBegin.addDays(1-dayOfWeek))
        self._count = dayOfWeek-1 + self._fromDate.daysInMonth()

        self.beginRemoveRows(QtCore.QModelIndex(), 0, 100)
        self.endRemoveRows()

        self.beginInsertRows(QtCore.QModelIndex(), 0, self._count - 1)
        self.endInsertRows()

        self.dateChange.emit()

        log.debug("current date: " + self._currentDate.toString())

    def setYear(self, year):
        log.debug("set year!" + str(year));
        currentDate = QtCore.QDateTime(QtCore.QDate(year, self.month, self.day), self._currentDate.time())
        self.change(currentDate)

    def setMonth(self, month):
        log.debug("set Month!" + str(month));
        currentDate = QtCore.QDateTime(QtCore.QDate(self.year, month, self.day), self._currentDate.time())
        self.change(currentDate)

    def setDay(self, day):
        log.debug("set day!" + str(day));
        currentDate = QtCore.QDateTime(QtCore.QDate(self.year, self.month, day), self._currentDate.time())
        self.change(currentDate)

    def setHour(self, hour):
        log.debug("set hour!" + str(hour));
        currentDate = QtCore.QDateTime(self._currentDate.date(), QtCore.QTime(hour, self.minute))
        self.change(currentDate)

    def setMinute(self, minute):
        log.debug("set minute!" + str(minute));
        currentDate = QtCore.QDateTime(self._currentDate.date(), QtCore.QTime(self.hour, minute))
        self.change(currentDate)

    def getYear(self):
        return self._currentDate.date().year()

    def getMonth(self):
        return self._currentDate.date().month()

    def getDay(self):
        return self._currentDate.date().day()

    def getDateInSeconds(self):
        return self._currentDate.toTime_t()

    def setDateInSeconds(self, seconds):
        date = QtCore.QDateTime.fromTime_t(seconds)
        log.debug("set seconds: " + date.toString());
        self.change(date)

    def getHour(self):
        return self._currentDate.time().hour()

    def getMinute(self):
        return self._currentDate.time().minute()

    month = QtCore.pyqtProperty("int", fget=getMonth, fset=setMonth, notify=dateChange)
    year = QtCore.pyqtProperty("int", fget=getYear, fset=setYear, notify=dateChange)
    day = QtCore.pyqtProperty("int", fget=getDay, fset=setDay, notify=dateChange)

    hour = QtCore.pyqtProperty("int", fget=getHour, fset=setHour, notify=dateChange)
    minute = QtCore.pyqtProperty("int", fget=getMinute, fset=setMinute, notify=dateChange)

    dateInSeconds = QtCore.pyqtProperty("int", fget=getDateInSeconds, fset=setDateInSeconds, notify=dateChange)
